package com.chart.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chart.chart.domian.vo.ChartDetailVO;
import com.chart.chart.domian.vo.ChartListsVO;
import com.chart.chart.domian.vo.ChartTypeVO;
import com.chart.chart.domian.vo.RelatedChartVO;
import com.chart.chart.mapper.ChartTypeMapper;
import com.chart.common.core.domain.entity.ChartList;
import com.chart.system.mapper.ChartListMapper;
import com.chart.system.service.ChartListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ChartList 服务实现类
 *
 * @author chart
 */
@Service
public class ChartListServiceImpl extends ServiceImpl<ChartListMapper, ChartList> implements ChartListService {

    @Autowired
    private ChartListMapper chartListMapper;

    @Autowired
    private ChartTypeMapper chartTypeMapper;


    @Override
    public List<ChartListsVO> getChartsByStatus() {
        // 创建 QueryWrapper 对象
        QueryWrapper<ChartList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1); // 查询状态为 1 的图表

        // 指定查询返回的字段
        queryWrapper.select("chart_code", "chart_name", "small_pic", "chart_instr");

        // 执行查询，返回图表列表
        List<ChartList> chartLists = chartListMapper.selectList(queryWrapper);

        // 将 ChartList 转换为 ChartListsVO
        List<ChartListsVO> chartListsVO = chartLists.stream()
                .map(chart -> {
                    ChartListsVO vo = new ChartListsVO();  // 使用无参构造函数// 设置字段
                    vo.setChartName(chart.getChartName());
                    vo.setSmallPic(chart.getSmallPic());
                    vo.setChartInstr(chart.getChartInstr());
                    vo.setChartCode(chart.getChartCode());
                    return vo;
                })
                .collect(Collectors.toList());

        return chartListsVO;
    }

    @Override
    public List<ChartTypeVO> getChartsByStatuss() {
        List<ChartTypeVO> chartTypeVOS=  chartTypeMapper.getChartsByStatuss();

        return chartTypeVOS;
    }





    @Override
    public ChartDetailVO getChartDetail(Integer chartCode) {
        // 查询图表的详细信息，并且要求 status=1
        ChartList chartDetail = chartListMapper.selectOne(new QueryWrapper<ChartList>()
                .eq("chart_code", chartCode)
                .eq("status", 1)
                .select("chart_code", "chart_name", "chart_desc", "data_amount", "default_data",
                        "default_config", "chart_path", "chart_config_ui", "chart_map_name", "chart_map_path", "relate_sort","is_complex"));

        ChartDetailVO chartDetailVO = null;
        if (chartDetail != null) {
            // 构造 ChartDetailVO
            chartDetailVO = new ChartDetailVO();
            chartDetailVO.setChartCode(chartDetail.getChartCode());
            chartDetailVO.setChartName(chartDetail.getChartName());
            chartDetailVO.setChartDesc(chartDetail.getChartDesc());
            chartDetailVO.setDataAmount(chartDetail.getDataAmount());
            chartDetailVO.setDefaultData(chartDetail.getDefaultData());
            chartDetailVO.setDefaultConfig(chartDetail.getDefaultConfig());
            chartDetailVO.setChartPath(chartDetail.getChartPath());
            chartDetailVO.setChartConfigUi(chartDetail.getChartConfigUi());
            chartDetailVO.setChartMapName(chartDetail.getChartMapName());
            chartDetailVO.setChartMapPath(chartDetail.getChartMapPath());
            chartDetailVO.setIsComplex(chartDetail.getIsComplex());
            // 获取 relate_sort 并判断是否为0
            Integer relateSort = chartDetail.getRelateSort();

            List<RelatedChartVO> relatedChartVOList;
            if (relateSort == 0) {
                // 如果 relate_sort 为 0，则 relateList 只包含图表本身
                RelatedChartVO selfChartVO = new RelatedChartVO();
                selfChartVO.setChartCode(chartDetail.getChartCode());
                selfChartVO.setChartName(chartDetail.getChartName());
                selfChartVO.setChartPath(chartDetail.getChartPath());
                selfChartVO.setChartConfigUi(chartDetail.getChartConfigUi());
                selfChartVO.setChartMapName(chartDetail.getChartMapName());
                selfChartVO.setChartMapPath(chartDetail.getChartMapPath());
                selfChartVO.setIsComplex(chartDetail.getIsComplex());
                relatedChartVOList = Collections.singletonList(selfChartVO);
            } else {
                // 否则查询所有相同 relate_sort 的图表，并要求 status=1
                List<ChartList> sameRelateSortCharts = chartListMapper.selectList(new QueryWrapper<ChartList>()
                        .eq("relate_sort", relateSort)
                        .eq("status", 1)
                        .select("chart_code", "chart_name", "chart_path", "chart_config_ui", "chart_map_name", "chart_map_path","is_complex"));

                // 构造与相同 relate_sort 相关的图表列表
                relatedChartVOList = new ArrayList<>();

                // 确保 relateList 的第一个元素是查询的图表本身
                RelatedChartVO selfChartVO = new RelatedChartVO();
                selfChartVO.setChartCode(chartDetail.getChartCode());
                selfChartVO.setChartName(chartDetail.getChartName());
                selfChartVO.setChartPath(chartDetail.getChartPath());
                selfChartVO.setChartConfigUi(chartDetail.getChartConfigUi());
                selfChartVO.setChartMapName(chartDetail.getChartMapName());
                selfChartVO.setChartMapPath(chartDetail.getChartMapPath());
                selfChartVO.setIsComplex(chartDetail.getIsComplex());
                relatedChartVOList.add(selfChartVO);

                // 添加其他相关图表到列表中，但排除自身以避免重复
                relatedChartVOList.addAll(sameRelateSortCharts.stream()
                        .filter(otherChart -> !otherChart.getChartCode().equals(chartCode)) // 排除自身
                        .map(chart -> {
                            RelatedChartVO relatedChartVO = new RelatedChartVO();
                            relatedChartVO.setChartCode(chart.getChartCode());
                            relatedChartVO.setChartName(chart.getChartName());
                            relatedChartVO.setChartPath(chart.getChartPath());
                            relatedChartVO.setChartConfigUi(chart.getChartConfigUi());
                            relatedChartVO.setChartMapName(chart.getChartMapName());
                            relatedChartVO.setChartMapPath(chart.getChartMapPath());
                            relatedChartVO.setIsComplex(chart.getIsComplex());
                            return relatedChartVO;
                        }).collect(Collectors.toList()));
            }

            // 设置关联图表列表到 VO 中
            chartDetailVO.setRelateList(relatedChartVOList);
        }

        return chartDetailVO;
    }




}